﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardGamesLibrary.Poker;

namespace CardGamesLibrary.UI.Poker
{
    public partial class PokerSeatControl : UserControl
    {
        private PokerSeat _pokerSeat;

        public PokerSeatControl()
        {
            InitializeComponent();

        }

        public void AssignSeat(PokerSeat pokerSeat)
        {
            _pokerSeat = pokerSeat;
            _pokerSeat.PropertyChanged += _pokerSeat_PropertyChanged;
            RefreshPlayerData();
        }

        void _pokerSeat_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            RefreshPlayerData();
        }

        private void RefreshPlayerData()
        {
           
            if(InvokeRequired)
            {
                base.Invoke((MethodInvoker) RefreshPlayerData);
                return;
            }
            if (IsOccupied)
            {
                playerName.Text = _pokerSeat.Player.Name;
                playerChips.Text = string.Format("{0} $", _pokerSeat.Chips);
            }
            else
            {
                playerName.Text ="";
                playerChips.Text = "";
            }
            if (_pokerSeat != null && _pokerSeat.Hand != null && _pokerSeat.Hand.Count == 2)
            {
                if(frenchDeckCardControl1!=null)
                frenchDeckCardControl1.SetCard(_pokerSeat.Hand[0]);
                if (frenchDeckCardControl2 != null)
                frenchDeckCardControl2.SetCard(_pokerSeat.Hand[1]);
            }
        }

        private void PokerSeatControl_MouseEnter(object sender, EventArgs e)
        {

            this.BackColor = IsOccupied ? Color.Red : Color.Green;
        }

        private void PokerSeatControl_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = Color.RosyBrown;
        }

        private void PokerSeatControl_Load(object sender, EventArgs e)
        {
            RefreshPlayerData();
        }

        public bool IsOccupied
        {
            get { return _pokerSeat != null && _pokerSeat.Player != null; }
        }
    }
}
